    program matched_price_size2
    character*6 ticker
    character*8 cusp
    integer(kind=8) nshrs,nvalue,jvalue,ivol,numshrs
    dimension nob(38),neps(100000,38),turn(100000,38),fsize(100000,38),iperm(100000,38),price(100000,38),ndate(100000,38)
    dimension fee(100000,38),pricem(100000,38),sizem(100000,38),turnm(100000,38),diff(100000,38),mperm(100000,38),ntype(1280)

!      open(unit=10,file='e:\FIS_duration\episode_data_exc.txt')
!      open(unit=11,file='e:\FIS\crsp_util_sorted_fees_2005_2023_exc.txt')
!      open(unit=12,file='e:\FIS_duration\episode_results.txt')
!      open(unit=99,file='e:\FIS_duration\episode_data_matched_turn_type.txt')
      open(unit=10,file='e:\FIS_duration\episode_data_exc_rep.txt')
      open(unit=11,file='e:\FIS\crsp_util_sorted_fees_2005_2023_exc_rep.txt')
      open(unit=12,file='e:\FIS_duration\episode_results_rep.txt')
      open(unit=99,file='e:\FIS_duration\episode_data_matched_turn_type_rep.txt')
      do 700 i=1,1264
700   read(12,1200)ntype(i)
1200  format(6x,i1)      
      nob=0
      diff=999.9
1      read(10,1000,end=10)nep,jast,jyr,jmn,jdy,cusp,ticker,itickt,numshrs,age,favg,fmax,fmin,fstd,iprm,prc,ivol,retrn,ishrout,vwret,util,nshrs,jcode     
1000   format(1x,i4,1x,i8,1x,i4,2i2,1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i1)
       pric=abs(prc)
       if(pric.eq.0.) go to 1
       if(ishrout.eq.0) go to 1
       jdt=jyr*10000+jmn*100+jdy
       sz=pric*float(ishrout)
       trn=float(ivol)/(1000*float(ishrout))
       np=jyr-2004
       if(jcode.eq.1.or.jcode.eq.2) np=np+19
       nob(np)=nob(np)+1
       neps(nob(np),np)=nep
       turn(nob(np),np)=trn
       fsize(nob(np),np)=sz
       iperm(nob(np),np)=iprm
       price(nob(np),np)=pric
       ndate(nob(np),np)=jdt
       fee(nob(np),np)=favg
       go to 1
10     continue
!       do 12 ii=1,38
!12         write(6,6000)nob(ii)
6000  format(1x,i6)           
20    read(11,1100,end=100)jast,jyr,jmn,jdy,cusp,ticker,itickt,numshrs,jvalue,age,favg,fmax,fmin,fstd,iprm,prc,ivol,retrn,ishrout,vwret,util,nshrs,nvalue,jcode      
!      write(6,1100)jast,jyr,jmn,jdy,cusp,ticker,itickt,numshrs,jvalue,age,favg,fmax,fmin,fstd,iprm,prc,ivol,retrn,ishrout,vwret,util,nshrs,nvalue,jcode      
1100  format(1x,i8,1x,i4,2i2,1x,a8,2x,a6,4x,i9,2(1x,i14),5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f9.6,1x,i9,1x,f9.6,1x,f6.2,2(1x,i13),1x,i1) 
      if(favg.gt.2.00) go to 20
      pric=abs(prc)
      jdt=jyr*10000+jmn*100+jdy
      np=jyr-2004
      if(jcode.eq.1.or.jcode.eq.2) np=np+19
      do 40 k=1,nob(np)
          if(jdt.ne.ndate(k,np)) go to 40
          if(iprm.eq.iperm(k,np)) go to 40
          dd=((pric-price(k,np))/price(k,np))**2
          if(dd.gt.diff(k,np)) go to 40
          dd=dd+((pric*float(ishrout)-fsize(k,np))/fsize(k,np))**2.
          if(dd.gt.diff(k,np)) go to 40
          diff(k,np)=dd
          mperm(k,np)=iprm
          sizem(k,np)=pric*float(ishrout)
          pricem(k,np)=pric
          turnm(k,np)=float(ivol)/(1000.*float(ishrout))
40    continue  
      go to 20
100   continue
      do 120 np=1,38
          kex=3
          if(np.gt.19) kex=1
          do 110 k=1,nob(np)
110     write(99,9900)neps(k,np),iperm(k,np),ndate(k,np),kex,fee(k,np),fsize(k,np),price(k,np),turn(k,np),mperm(k,np),sizem(k,np),pricem(k,np),diff(k,np),turnm(k,np),ntype(neps(k,np))
9900    format(1x,i4,1x,i5,1x,i8,1x,i1,1x,f10.4,1x,f12.1,1x,f9.2,1x,f9.6,1x,i5,1x,f12.1,1x,f9.2,1x,f10.7,1x,f9.6,1x,i1)
120   continue        
900   iend=iend+1
      stop

    end program matched_price_size2
